package com.harassment.interception.ui.activities.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.harassment.interception.R;
import com.harassment.interception.data.databases.HIDBUtil;
import com.harassment.interception.data.databases.HIDatabase;
import com.harassment.interception.data.databases.entities.BlackList;
import com.harassment.interception.data.databases.entities.InterceptedPhone;
import com.harassment.interception.data.databases.entities.InterceptedSms;
import com.harassment.interception.data.databases.entities.WhiteList;
import com.harassment.interception.ui.fragments.bottomFragment.BottomBar;

import java.util.Date;

import io.reactivex.Completable;
import io.reactivex.CompletableEmitter;
import io.reactivex.CompletableOnSubscribe;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private BottomBar mBottomBar;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initActionBar();
        initBottomBar();
        PreferenceManager.setDefaultValues(this,R.xml.fragment_setting,false);
//        initDatabase();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_activity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.record:
                RecordShowActivity.startRecordShowActivity(this);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initBottomBar(){
        if (mBottomBar != null) {
            return;
        }
        mBottomBar = new BottomBar(this);
    }


    private void initActionBar(){
        if (mToolbar == null) {
            mToolbar = findViewById(R.id.base_action_bar);
        }
        super.setSupportActionBar(mToolbar);
    }

    @SuppressLint("CheckResult")
    private void initDatabase(){
        Completable.create(new CompletableOnSubscribe() {
            @Override
            public void subscribe(CompletableEmitter emitter) throws Exception {
                InterceptedPhone[] interceptedPhones = new InterceptedPhone[3];
                interceptedPhones[0] = new InterceptedPhone();
                interceptedPhones[1] = new InterceptedPhone();
                interceptedPhones[2] = new InterceptedPhone();
                interceptedPhones[0].setNumber("12345678910");
                interceptedPhones[1].setNumber("01987654321");
                interceptedPhones[2].setNumber("888888888888");
                interceptedPhones[0].setDateTime(new Date(System.currentTimeMillis()));
                interceptedPhones[1].setDateTime(new Date(System.currentTimeMillis()));
                interceptedPhones[2].setDateTime(new Date(System.currentTimeMillis()));
                interceptedPhones[0].setRemark("备注123");
                interceptedPhones[1].setRemark("备注456");
                interceptedPhones[2].setRemark("备注789");
                HIDBUtil.getInterceptedPhoneDao().addInterceptedPhone(interceptedPhones);

                InterceptedSms[] interceptedSms = new InterceptedSms[3];
                interceptedSms[0] = new InterceptedSms();
                interceptedSms[1] = new InterceptedSms();
                interceptedSms[2] = new InterceptedSms();
                interceptedSms[0].setNumber("888888888888");
                interceptedSms[1].setNumber("01987654321");
                interceptedSms[2].setNumber("12345678910");
                interceptedSms[0].setContent("我加里萨娇滴滴骄傲技术机机啊送i额技术列娃iu机哦了里啊额给阿迪带上");
                interceptedSms[1].setContent("我加里萨娇滴滴骄傲技术机机啊送i额技术列娃iu机哦了里啊额给阿迪带上");
                interceptedSms[2].setContent("我加里萨娇滴滴骄傲技术机机啊送i额技术列娃iu机哦了里啊额给阿迪带上");
                interceptedSms[0].setDateTime(new Date(System.currentTimeMillis()));
                interceptedSms[1].setDateTime(new Date(System.currentTimeMillis()));
                interceptedSms[2].setDateTime(new Date(System.currentTimeMillis()));
                interceptedSms[0].setRemark("备注123");
                interceptedSms[1].setRemark("备注456");
                interceptedSms[2].setRemark("备注789");
                HIDBUtil.getInterceptedSmsDao().addInterceptedSms(interceptedSms);

                BlackList[] blackLists = new BlackList[3];
                blackLists[0] = new BlackList();
                blackLists[1] = new BlackList();
                blackLists[2] = new BlackList();
                blackLists[0].setNumber("01987654321");
                blackLists[1].setNumber("888888888888");
                blackLists[2].setNumber("12345678910");
                blackLists[0].setRemark("备注123");
                blackLists[1].setRemark("备注456");
                blackLists[2].setRemark("备注789");
                blackLists[0].setPhone(true);
                blackLists[1].setPhone(false);
                blackLists[2].setPhone(true);
                blackLists[0].setSms(true);
                blackLists[1].setSms(true);
                blackLists[2].setSms(false);
                blackLists[0].setModel(HIDatabase.ACCURATE_MODEL);
                blackLists[1].setModel(HIDatabase.ACCURATE_MODEL);
                blackLists[2].setModel(HIDatabase.DARK_MODEL);
                HIDBUtil.getBlackListDao().addBlackList(blackLists);

                WhiteList[] whiteLists = new WhiteList[3];
                whiteLists[0] = new WhiteList();
                whiteLists[1] = new WhiteList();
                whiteLists[2] = new WhiteList();
                whiteLists[0].setNumber("01987654321");
                whiteLists[1].setNumber("12345678910");
                whiteLists[2].setNumber("888888888888");
                whiteLists[0].setRemark("备注123");
                whiteLists[1].setRemark("备注456");
                whiteLists[2].setRemark("备注789");
                whiteLists[0].setPhone(true);
                whiteLists[1].setPhone(false);
                whiteLists[2].setPhone(true);
                whiteLists[0].setSms(true);
                whiteLists[1].setSms(true);
                whiteLists[2].setSms(false);
                whiteLists[0].setModel(HIDatabase.ACCURATE_MODEL);
                whiteLists[1].setModel(HIDatabase.ACCURATE_MODEL);
                whiteLists[2].setModel(HIDatabase.DARK_MODEL);
                HIDBUtil.getWhiteListDao().addWhiteList(whiteLists);

                Log.d(TAG, "subscribe: add database");
                emitter.onComplete();
            }
        })
                .subscribeOn(Schedulers.io())
                .subscribe(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d(TAG, "run: add database finish");
                    }
                });
    }

    public BottomBar getBottomBar() {
        return mBottomBar;
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }
}
